<?php

/*  PHP Paypal IPN Integration Class Demonstration File
 *  4.16.2005 - Micah Carrick, email@micahcarrick.com
 *
 *  This file demonstrates the usage of paypal.class.php, a class designed  
 *  to aid in the interfacing between your website, paypal, and the instant
 *  payment notification (IPN) interface.  This single file serves as 4 
 *  virtual pages depending on the "action" varialble passed in the URL. It's
 *  the processing page which processes form data being submitted to paypal, it
 *  is the page paypal returns a user to upon success, it's the page paypal
 *  returns a user to upon canceling an order, and finally, it's the page that
 *  handles the IPN request from Paypal.
 *
 *  I tried to comment this file, aswell as the acutall class file, as well as
 *  I possibly could.  Please email me with questions, comments, and suggestions.
 *  See the header of paypal.class.php for additional resources and information.
 */

// Setup class

$root_path = "";

require("required/dbconnect.php");
require("required/email.php");
require("required/share_function.php");
include('ipnlistener.php');

$get_info = mysql_query("select * from os_contactinfo where id=1");
$i = mysql_fetch_array($get_info, MYSQL_ASSOC);

$paypal_submit_url = $i['paypal_submit_url'];

ini_set('log_errors', true);
ini_set('error_log', dirname(__FILE__) . 'ipn_errors.log');
//echo  dirname(__FILE__) . '\ipn_errors.log';
/*
  When you are testing your IPN script you should be using a PayPal "Sandbox"
  account: https://developer.paypal.com
  When you are ready to go live change use_sandbox to false.
 */
$listener = new IpnListener();
$listener->use_sandbox = true;

try {
    $listener->requirePostMethod();
    $verified = $listener->processIpn();
} catch (Exception $e) {
    error_log($e->getMessage());
    exit(0);
}

/*
  The processIpn() method returned true if the IPN was "VERIFIED" and false if it
  was "INVALID".
 */

if ($verified) {



    //mysql_query("insert into os_payment_record(paypal_ref_id,ipn_values,create_datetime)values('$text','" . $listener->getTextReport() . "',now())") or die(mysql_error());
    $invoice_no = $listener->post_data['invoice'];
    $payer_email = $listener->post_data['payer_email'];
    $mc_gross = $listener->post_data['mc_gross'];
    $txn_id = $listener->post_data['txn_id'];
    $ipn_track_id = $listener->post_data['ipn_track_id'];
    $payer_id = $listener->post_data['payer_id'];

    $get_bid = mysql_query("select id from os_bill_details where invoice_number='$invoice_no'");

    if (mysql_num_rows($get_bid)) {
        $bid = mysql_result($get_bid, 0, "id");
        $uid = mysql_result($get_bid, 0, "uid");
    } else {
        $bid = "0";
        $uid = "0";
    }

    mysql_query("insert into os_payment_record(uid,bid,invoice_no,paidamount,paymentdate,paymentbank,payer_paypal_email,payer_id,mc_gross,txn_id,ipn_track_id,ipn_values,create_datetime,is_paypal)values
   ('$uid',$bid','$invoice_no','$mc_gross',now(),'paypal','$payer_email','$payer_id','$mc_gross','$txn_id','$ipn_track_id','" . $listener->getTextReport() . "',now(),1)");
    
    mysql_query("update os_bill_details set status='-4' where id='$bid'") or die(mysql_error());


    $get_items = mysql_query("select a.id,a.item_id,a.quantity,b.pid from os_bill_items a inner join os_product_inventory b on a.item_id = b.id where a.bid=$bid");

    while ($i = mysql_fetch_array($get_items, MYSQL_ASSOC)) {
        $item_id = $i['item_id'];
        $total_deduct = $i['quantity'];
        $pid = $i['pid'];
        updateInventory($item_id, $total_deduct, $bid, $pid);
    }

    email_paypal_payment_received($invoice_no);
} else {
    /*
      An Invalid IPN *may* be caused by a fraudulent transaction attempt. It's
      a good idea to have a developer or sys admin manually investigate any
      invalid IPN.
     */
    mysql_query("insert into os_payment_record(ipn_values,create_datetime)values('" . $listener->getTextReport() . "',now())") or die(mysql_error());
}
?>